Imports System.IO
Imports System.Data.SqlClient
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Public Class FrmSecurity

    Public Class ColoredTextBoxColumn
        ' ColoredTextBoxColumn is derived from the DataGridTextBoxColumn class.
        Inherits DataGridTextBoxColumn
        Private mMyDataGrid As DataGrid
        Public Property OwnerDG() As DataGrid
            Get
                Return mMyDataGrid
            End Get
            Set(ByVal Value As DataGrid)
                mMyDataGrid = Value
            End Set
        End Property
        Protected Overloads Overrides Sub Paint(ByVal graph As Graphics, _
            ByVal rectbounds As Rectangle, ByVal curmngrSrc As _
            CurrencyManager, ByVal RowNumber As Integer, ByVal _
            ForeColorBrush As Brush, ByVal BackColorBrush As Brush, _
            ByVal AlignmentRight As Boolean)

            Dim ObjVal As Object
            ObjVal = Me.GetColumnValueAtRow(curmngrSrc, RowNumber)

            If Not (IsNothing(ObjVal) Or IsDBNull(ObjVal)) Then
                If Me.OwnerDG.CurrentRowIndex = RowNumber Then
                    BackColorBrush = Brushes.Yellow
                    ForeColorBrush = Brushes.Red
                Else
                    BackColorBrush = Brushes.White
                    ForeColorBrush = Brushes.Black
                End If
            End If

            MyBase.Paint(graph, rectbounds, curmngrSrc, RowNumber, _
                BackColorBrush, ForeColorBrush, AlignmentRight)
        End Sub

    End Class

    Private Sub FrmSecurity_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'TODO: This line of code loads data into the 'B2BDataSet.Users' table. You can move, or remove it, as needed.
        LoadDataGrid()
        '   ListAllPermission()
        Dim myCell As New DataGridCell()
        ' Use an arbitrary cell.
        myCell.RowNumber = DataGrid1.CurrentRowIndex
        myCell.ColumnNumber = 0
        GV_SEL_PERUSERID = DataGrid1(myCell)
        ListUserPermission(GV_SEL_PERUSERID)


    End Sub
    Private Sub ListUserPermission(ByVal vUserID)
        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("ListUserPermission " & vUserID, SqlConnection1)
        ' sqlCmd.Parameters.Add("@UserId", SqlDbType.VarChar)
        ' sqlCmd.Parameters("@UserId").Value = GV_USER

        Try
            ChkListBoxRights.Items.Clear()
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Dim i As Integer
            i = 1
            Do While objReader.Read
                ChkListBoxRights.Items.Add(objReader("PermissionDescription"), IIf(IsDBNull(objReader("enteredTime")), False, True))
                i = i + 1
            Loop
        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()

    End Sub
    Private Sub ListAllPermission()
        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("ListAllPermission", SqlConnection1)

        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Dim i As Integer
            i = 1
            Dim list As New DataTable
            list.Columns.Add(New DataColumn("PermissionId", GetType(String)))
            list.Columns.Add(New DataColumn("PermissionDescription", GetType(String)))
            list.Rows.Add(list.NewRow())
            list.Rows(0)(0) = ""
            list.Rows(0)(1) = ""
            Do While objReader.Read
                list.Rows.Add(list.NewRow())
                list.Rows(i)(0) = IIf(IsDBNull(objReader("PermissionDescription")), "", objReader("PermissionDescription"))
                list.Rows(i)(1) = IIf(IsDBNull(objReader("PermissionId")), "", objReader("PermissionId"))
                ChkListBoxRights.Items.Add(objReader("PermissionDescription"))
                i = i + 1
            Loop
            'ChkListBoxRights.DataSource = list
            'ChkListBoxRights.DisplayMember = "PermissionDescription"
            'ChkListBoxRights.ValueMember = "PermissionId"


        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()

    End Sub



    Private Sub LoadDataGrid()

        Dim cnSQL As New SqlConnection(GV_CONNETION)
        cnSQL.Open()

        Dim sqlCmd = New SqlCommand("ListActiveUser", cnSQL)
        sqlCmd.CommandType = CommandType.StoredProcedure
        Dim sqlAdp = New SqlDataAdapter(sqlCmd)
        Dim ApptList_Data = New DataSet()
        sqlAdp.Fill(ApptList_Data, "Users")


        '----------------------------------------------
        Dim ts As New DataGridTableStyle()
        ts.MappingName = ApptList_Data.Tables(0).TableName
        With ts
            .MappingName = "Users"
            .BackColor = System.Drawing.Color.White
            .ForeColor = System.Drawing.Color.DarkSlateBlue
            .GridLineColor = System.Drawing.Color.MediumSlateBlue
            .HeaderBackColor = System.Drawing.Color.Lavender
            .HeaderForeColor = System.Drawing.Color.SlateBlue
            .RowHeadersVisible = False
            .PreferredRowHeight = 15
        End With

        '...highlight row 
        Dim numCols As Integer
        numCols = ApptList_Data.Tables(0).Columns.Count
        Dim aColumnTextColumn As ColoredTextBoxColumn 'DataGridTextBoxColumn

        Dim i As Integer
        i = 0

        Do While (i < numCols)
            aColumnTextColumn = New ColoredTextBoxColumn()
            aColumnTextColumn.OwnerDG = Me.DataGrid1
            ' aColumnTextColumn.ConditionalText = Me.tbFind
            Select Case i
                Case 0

                    aColumnTextColumn.HeaderText = "userId"
                    aColumnTextColumn.MappingName = "userId"
                    aColumnTextColumn.Width = 0
                Case 1
                    aColumnTextColumn.HeaderText = "User Name"
                    aColumnTextColumn.MappingName = "UserName"
                    aColumnTextColumn.Width = 250
            End Select
            ts.GridColumnStyles.Add(aColumnTextColumn)
            i = (i + 1)
        Loop


        'DataGrid1.DataBindings.Add(ApptList_Data)
        DataGrid1.TableStyles.Clear()     'clear the contents for second try.
        DataGrid1.TableStyles.Add(ts)
        DataGrid1.DataSource = ApptList_Data.Tables("Users")


    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Close()
    End Sub

    Private Sub ChkListBoxRights_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkListBoxRights.SelectedIndexChanged

    End Sub

    Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
        Dim myCell As New DataGridCell()
        ' Use an arbitrary cell.
        myCell.RowNumber = DataGrid1.CurrentRowIndex
        myCell.ColumnNumber = 0
        GV_SEL_PERUSERID = DataGrid1(myCell)
        ListUserPermission(GV_SEL_PERUSERID)
    End Sub

    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Try
            ClearUserPermission()
            For Each Item As Object In ChkListBoxRights.CheckedItems
                AddNewUserPermission(Item.ToString)
            Next
            MsgBox("Data saved.", MsgBoxStyle.Information, "Save")

        Catch ex As Exception
            Exit Sub
        End Try
    End Sub
    Private Sub ClearUserPermission()
        Dim myCommand As SqlCommand
        Dim SqlConn As New SqlConnection()
        SqlConn = New SqlConnection(GV_CONNETION)
        myCommand = New SqlCommand("spClearUserPermission", SqlConn)
        myCommand.CommandType = CommandType.StoredProcedure
        SqlConn.Open()
        myCommand.Parameters.Add("@PermissionUserId", SqlDbType.VarChar)
        myCommand.Parameters.Add("@EnteredBy", SqlDbType.VarChar)
        myCommand.Parameters("@PermissionUserId").Value = GV_SEL_PERUSERID
        myCommand.Parameters("@EnteredBy").Value = GV_USER
        myCommand.ExecuteNonQuery()

        myCommand.Cancel()
        SqlConn.Close()
        myCommand = Nothing
        SqlConn = Nothing
    End Sub
    Private Sub AddNewUserPermission(ByVal vPermission)

        Dim myCommand As SqlCommand
        Dim SqlConn As New SqlConnection()
        SqlConn = New SqlConnection(GV_CONNETION)
        myCommand = New SqlCommand("spAddNewUserPermission", SqlConn)
        myCommand.CommandType = CommandType.StoredProcedure
        SqlConn.Open()
        myCommand.Parameters.Add("@PermissionUserId", SqlDbType.VarChar)
        myCommand.Parameters.Add("@PermissionDesc", SqlDbType.VarChar)
        myCommand.Parameters.Add("@EnteredBy", SqlDbType.VarChar)
        myCommand.Parameters("@PermissionUserId").Value = GV_SEL_PERUSERID
        myCommand.Parameters("@PermissionDesc").Value = vPermission
        myCommand.Parameters("@EnteredBy").Value = GV_USER
        myCommand.ExecuteNonQuery()

        myCommand.Cancel()
        SqlConn.Close()
        myCommand = Nothing
        SqlConn = Nothing

    End Sub

End Class

